package Servlet;

import DAO.MessageDAO;
import net.sf.json.JSONArray;
import pojo.Article;
import pojo.ArticleItem;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name= "LoadArticleServlet",value = "/LoadArticleServlet")
public class LoadArticleServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        MessageDAO messageDAO  = new MessageDAO();

        List<ArticleItem> articleList = messageDAO.LoadArticle();
        if(0 == articleList.size()){
            response.getWriter().write("null");
        } else{
            JSONArray json= JSONArray.fromObject(articleList);
            response.getWriter().print(json);
        }
        response.getWriter().flush();
        response.getWriter().close();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}
